<template>
  <el-dialog
    :title="!dataForm.id ? '预约' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
             label-width="80px">
      <el-form-item label="用户名" prop="username">
        <el-input v-model="dataForm.username" placeholder="用户名"></el-input>
      </el-form-item>
<!--      <el-form-item label="输入项目名" prop="teachername">-->
<!--        <el-input v-model="dataForm.teachername" placeholder="最好是匹配的项目名哦"></el-input>-->
<!--      </el-form-item>-->
      <el-form-item label="选择项目名" prop="coursename">
        <el-cascader
          v-model="dataForm.coursename"
          :options="options"
          @click="handleChange"></el-cascader>
      </el-form-item>
      <!--    <el-form-item label="创建时间" prop="createtime">-->
      <!--      <el-input v-model="dataForm.createtime" placeholder="创建时间"></el-input>-->
      <!--    </el-form-item>-->
      <!--      <el-form-item label="备注" prop="memo">-->
      <!--        <el-input v-model="dataForm.memo" placeholder="备注"></el-input>-->
      <!--      </el-form-item>-->
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      value: '',
      visible: false,
      options: [],
      dataForm: {
        id: 0,
        username: '',
        teachername: '',
        coursename: '',
        createtime: '',
        memo: ''
      },
      dataRule: {
        username: [
          {required: true, message: '用户名不能为空', trigger: 'blur'}
        ],
        teachername: [
          {required: true, message: '项目名不能为空', trigger: 'blur'}
        ],
        coursename: [
          {required: true, message: '项目名名不能为空', trigger: 'blur'}
        ],
        createtime: [
          {required: true, message: '创建时间不能为空', trigger: 'blur'}
        ],
        value: [
          {required: true, message: '项目名不能为空', trigger: 'blur'}
        ]
      }
    }
  },
  created() {
    this.handleChange()
  },
  methods: {
    handleChange() {
      this.$http({
        url: this.$http.adornUrl(`/jsf/subject/list1`),
        method: 'get'
      }).then(({data}) => {
        this.options = data.data
      })
    },
    init(id) {
      this.dataForm.id = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(`/jsf/yuyue/info/${this.dataForm.id}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.dataForm.username = data.yuyue.username
              this.dataForm.teachername = data.yuyue.teachername
              this.dataForm.coursename = data.yuyue.coursename
              this.dataForm.createtime = data.yuyue.createtime
              this.dataForm.memo = data.yuyue.memo
            }
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/jsf/yuyue/${!this.dataForm.id ? 'save' : 'update'}`),
            method: 'post',
            data: this.$http.adornData({
              'id': this.dataForm.id || undefined,
              'username': this.dataForm.username,
              'teachername': this.dataForm.teachername,
              'coursename': this.dataForm.coursename[0],
              'createtime': this.dataForm.createtime,
              'memo': this.dataForm.memo
            })
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    }
  }
}
</script>
